package com.mv.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mv.mapper.ConsumeMapper;
import com.mv.mapper.DealMapper;
import com.mv.pojo.Consume;
import com.mv.pojo.Deal;
import com.mv.pojo.Movie;
import com.mv.vo.MovieRecord;
import com.mysql.cj.protocol.x.CompressionSplittedOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ConsumeServiceImpl implements ConsumeService{
    @Autowired
    private MovieService movieService;
    @Autowired
    private DealService dealService;
    @Autowired
    private DealMapper dealMapper;
    @Autowired
    private ConsumeMapper consumeMapper;
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat dealTimeFormat = new SimpleDateFormat("HH:mm:ss");
    @Transactional
    @Override
    public void deleteConsumeByDealId(Integer dealId) {
        QueryWrapper<Consume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deal_id", dealId);
        consumeMapper.delete(queryWrapper);
    }

    @Override
    public List<Consume> getConsumeList() {
        int i=0;
        List<Consume> consumeList=new ArrayList<>();
        Map<Date,List<Consume>> map=getMap();
        for (Date date : map.keySet()) {
            consumeList.add(new Consume().setId(i--).setDate(date).setChildren(map.get(date)));
        }
        return consumeList;
    }

    @Override
    public List<MovieRecord> getMovieRecordList() {
        List<MovieRecord> movieRecordList=new ArrayList<>();
        Double moneySum = dealMapper.getConsumeMoneySum();
        Integer numSum=consumeMapper.getNumSum();
        List<Movie> movieList = movieService.getMovieList();
        for (Movie movie : movieList) {
            MovieRecord movieRecord = new MovieRecord();
            Integer num=consumeMapper.getMovieNum(movie.getId());
            if(num==null){
                continue;
            }
            Double money=num*movie.getPrice();
            Integer numProprotion=num*100/numSum;
            int moneyProprotion= (int) (money*100/moneySum);
            movieRecord.setMovieName(movie.getName())
                    .setNum(num)
                    .setMoney(money)
                    .setNumProprotion(numProprotion)
                    .setMoneyProprotion(moneyProprotion);
            movieRecordList.add(movieRecord);
        }
        return movieRecordList;
    }

    private Map<Date, List<Consume>> getMap() {
        Map<Date, List<Consume>> map=new HashMap<>();
        List<Consume> consumeList = findConsumeList();
        for (Consume consume : consumeList) {
            Date date = consume.getDate();
            if(map.containsKey(date)){
                map.get(date).add(consume);
            }else {
                ArrayList<Consume> list = new ArrayList<>();
                list.add(consume);
                map.put(date,list);
            }
        }
        return map;
    }

    private List<Consume> findConsumeList() {
        List<Consume> consumeList = consumeMapper.selectList(null);
        for (Consume consume : consumeList) {
            String movieName = movieService.getMovie(consume.getMovieId()).getName();
            String userName = dealService.getUserNameById(consume.getDealId());
            Deal deal = dealService.getDealById(consume.getDealId());
            String[] json = format.format(deal.getDealTime()).split(" ");
            Date date=null;
            Date dealTime=null;
            try {
                date = dateFormat.parse(json[0]);
                dealTime = dealTimeFormat.parse(json[1]);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            consume.setCardId(deal.getCardId())
                    .setUserName(userName)
                    .setMovieName(movieName)
                    .setMoney(deal.getMoneyChange())
                    .setDate(date).setDealTime(dealTime);
        }
        return consumeList;
    }
}
